// Shapes.cpp: implementation of the CShape class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"

#include "Shapes.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

IMPLEMENT_DYNAMIC(CShape, CObject)

CShape::CShape(UINT uPenWidth, UINT uPenStyle, 
		   COLORREF colorPen, COLORREF colorBrush)
{
	m_uPenWidth = uPenWidth;
	m_uPenStyle = uPenStyle;
	m_colorPen = colorPen;
	m_colorBrush = colorBrush;
}

CShape::~CShape()
{

}


void CShape::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
		ar << m_uPenWidth << m_uPenStyle;
		ar << m_colorPen << m_colorBrush;
	}
	else
	{
		// TODO: add loading code here
		ar >> m_uPenWidth >> m_uPenStyle;
		ar >> m_colorPen >> m_colorBrush;
	}
	m_points.Serialize(ar);
}

void CShape::Draw(CDC* pDC)
{
	CPen pen(m_uPenStyle, m_uPenWidth, m_colorPen);
	CPen* pOldPen = pDC->SelectObject(&pen);
	CBrush brush(m_colorBrush);
	CBrush* pOldBrush = pDC->SelectObject(&brush);

	OnDrawContent(pDC);

	pDC->SelectObject(pOldPen);
	pDC->SelectObject(pOldBrush);
}

//////////////////////////////////////////////////////////////////////
// CLine Class
//////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

IMPLEMENT_SERIAL(CLine, CShape, 0)

CLine::CLine(UINT uPenWidth, UINT uPenStyle, COLORREF colorPen) :
CShape(uPenWidth, uPenStyle, colorPen)
{

}

CLine::~CLine()
{

}

void CLine::OnDrawContent(CDC* pDC)
{
	INT_PTR nSize = m_points.GetSize();
	if(nSize < 2) return;
	pDC->MoveTo(m_points[0]);
	pDC->LineTo(m_points[1]);
}

BOOL CLine::StartEdit(CDC* pDC, POINT pt)
{
	m_points.Add(pt);
	m_points.Add(pt);
	return TRUE;
}

BOOL CLine::OnMouseMove(CDC* pDC, UINT nFlags, CPoint point)
{
	CPen pen(PS_SOLID, 1, RGB(255, 255, 255));
	CPen* pOldPen = pDC->SelectObject(&pen);
	int nROP2 = pDC->SetROP2(R2_XORPEN);

	OnDrawContent(pDC);

	m_points[1] = point;

	OnDrawContent(pDC);

	pDC->SelectObject(pOldPen);
	pDC->SetROP2(nROP2);

	return TRUE;
}

//////////////////////////////////////////////////////////////////////
// CRectangle Class
//////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

IMPLEMENT_SERIAL(CRectangle, CShape, 0)

CRectangle::CRectangle(UINT uPenWidth, UINT uPenStyle, COLORREF colorPen, COLORREF colorBrush) :
CShape(uPenWidth, uPenStyle, colorPen, colorBrush)
{

}

CRectangle::~CRectangle()
{

}

void CRectangle::OnDrawContent(CDC* pDC)
{
	INT_PTR nSize = m_points.GetSize();
	if(nSize < 2) return;

	pDC->Rectangle(m_points[0].x, m_points[0].y, m_points[1].x, m_points[1].y);
}

BOOL CRectangle::StartEdit(CDC* pDC, POINT pt)
{
	m_points.Add(pt);
	m_points.Add(pt);
	return TRUE;
}

BOOL CRectangle::OnMouseMove(CDC* pDC, UINT nFlags, CPoint point)
{
	CPen pen(PS_SOLID, 1, RGB(255, 255, 255));
	CPen* pOldPen = pDC->SelectObject(&pen);
	CGdiObject* pOldBrush=pDC->SelectStockObject(NULL_BRUSH);
	int nROP2 = pDC->SetROP2(R2_XORPEN);

	OnDrawContent(pDC);

	m_points[1] = point;

	OnDrawContent(pDC);

	pDC->SelectObject(pOldPen);
	pDC->SelectObject(pOldBrush);
	pDC->SetROP2(nROP2);

	return TRUE;
}
